# -*- coding:utf-8 -*-

from common.my_http import MyHttp
import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import threading
import hashlib
import urllib.parse
import time
import timedelta
import datetime
import requests
from common.my_time import MyTime
from common.my_file import MyFile
from common.attr_v3 import AttrV3
from database.db_handler import MysqlHander
import json
import os
from selenium import webdriver
import platform
from webdriver_base import WebdriverBase

'''
北交所监管数据
'''

'''
chromedriver = ""

options = webdriver.ChromeOptions()
if platform.system() == "Linux":
    chromedriver = "/usr/bin/chromedriver"
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
else:
    chromedriver = "D:\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options = options)
'''

class BjJianguanData(WebdriverBase):
    def __init__(self):
        self.v3 = AttrV3()
        self.file = open('./txt/' + os.path.split(__file__)[-1].split(".")[0] + ".txt", 'w', encoding = 'utf-8')
        WebdriverBase.__init__(self, './log/' + os.path.split(__file__)[-1].split(".")[0] + ".log")

    def __del__(self):
        self.file.close()
        pass

    def query_page_data(self, page):
        print("query_page_data")
        time.sleep(10)
        trs = self.driver.find_element_by_tag_name("tbody").find_elements_by_tag_name("tr")
        for tr in trs:
            print("add data")
            tds = tr.find_elements_by_tag_name("td")
            data_code = tds[0].text
            if "000000" == data_code:
                continue
            data_url = tds[3].find_element_by_tag_name("a").get_attribute("href")
            data_title = tds[3].find_element_by_tag_name("a").text
            data_date = tds[5].text
            table = self.v3.get_table_name(3002)
            try:
                self.v3.add_attr_data(table, data_code, data_title, data_date, data_url, "北交所")
            except Exception as e:
                self.write_log(str(e))
                pass
            self.file.write(data_code + "|0|3002|" + data_title + "|" + data_date + "|" + data_url + "|北交所|\n")
 
    def query_data(self):
        self.driver.get("http://www.bse.cn/disclosure/supervise.html")
        page = 0
        self.query_page_data(page)
        self.driver.quit()
    
    def get_data(self):
        self.query_data()


def beijiaosuo_jianguan_api():
    d = BjJianguanData()
    d.get_data()

if __name__ == '__main__':
    d = BjJianguanData()
    d.get_data()

